package com.yunmang.utils;

import android.content.Context;
import android.text.TextUtils;

import com.yunmang.app.AppConstant;
import com.yunmang.entity.ExceptionBean;
import com.yunmang.entity.ParameterEntity.ConfirmBean;
import com.yunmang.entity.TransportBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/18.
 */
public class ScanUtils{
    /**
     * 将单号加入对应的文件并重组文件
     * @param context
     * @param spName 查看的文件名
     * @param scanHdbh 单号
     */
    public static void operateFile(Context context, String spName, String scanHdbh){
        String history=SharedPreferencesUtils.getParameter(context,spName);
        String[] histroyArray=history.split(",");
        List<String> historyList=new ArrayList<String>(Arrays.asList(histroyArray));
        if(historyList.size()>0){
            if(historyList.contains(scanHdbh)){//含有改单号，需要给提示
                historyList.remove(scanHdbh);
            }
            historyList.add(0,scanHdbh);
        }
        //加到集合之后，我们需要在加上分隔符，然后存入文件中
        if(historyList.size()>0){
            StringBuilder builder=new StringBuilder();
            for (int i = 0; i <historyList.size();i++) {
                builder.append(historyList.get(i)+",");
            }
            SharedPreferencesUtils.saveParameter(context,spName,builder.toString());
        }else{//如果之前没有数据
            SharedPreferencesUtils.saveParameter(context,spName,scanHdbh+",");
        }
    }

    /**
     * 扫描下载的时候先检测文件中是否存在该单号
     * @param context
     * @param spName
     * @param scanHdbh
     * @return
     */
    public static boolean isExistScanHdbh(Context context,String spName,String scanHdbh){
        String history=SharedPreferencesUtils.getParameter(context,spName);
        String[] histroyArray=history.split(",");
        List<String> historyList=new ArrayList<String>(Arrays.asList(histroyArray));
        if(historyList.contains(scanHdbh)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查询异常单号
     * @param list
     * @return
     */
    public static ExceptionBean initExceptionGoods(List<TransportBean.ModelBean.ItemsBean>  list){
        ExceptionBean exBean = new ExceptionBean();
        ArrayList<String> exceptionGoods,temptotal;
        exceptionGoods = new ArrayList<String>();
        temptotal = new ArrayList<String>();
        for (TransportBean.ModelBean.ItemsBean bean : list) {
            if(!bean.getSaomiaonum().equals(bean.getNumbers())){
                exceptionGoods.add(bean.getHdbh());
                temptotal.add(bean.getNumbers());
            }
        }
        exBean.setExceptionGoods(exceptionGoods);
        exBean.setTemptotal(temptotal);
        return exBean;
    }


    /**
     * 查询异常单号
     * @param list
     * @return
     */
    public static ExceptionBean initExceptionGoodsForArrive(List<ConfirmBean.ModelBean.ItemsBean>  list){
        ExceptionBean exBean = new ExceptionBean();
        ArrayList<String> exceptionGoods,temptotal;
        exceptionGoods = new ArrayList<String>();
        temptotal = new ArrayList<String>();
        for (ConfirmBean.ModelBean.ItemsBean bean : list) {
            if(!bean.getSaomiaonum().equals(bean.getNumbers())){
                exceptionGoods.add(bean.getHdbh());
                temptotal.add(bean.getNumbers()+"");
            }
        }
        exBean.setExceptionGoods(exceptionGoods);
        exBean.setTemptotal(temptotal);
        return exBean;
    }

    /**
     * 遍历数据，是否允许上传
     * @param list
     * @return
     */
    public static boolean isUploadData(List<TransportBean.ModelBean.ItemsBean>  list){
        if(list==null||list.size()==0){
            return false;
        }
        boolean flag=true;
        for (TransportBean.ModelBean.ItemsBean bean : list) {
            if(bean.isSelect()){
                int num=Integer.parseInt(bean.getNumbers());
                int saomiaonum=Integer.parseInt(bean.getSaomiaonum());
                if(num< AppConstant.maxScanNum){
                    if(num!=saomiaonum)
                        return false;
                }
            }
        }
        return flag;
    }



    /**
     * 遍历数据，是否允许上传
     * @param list
     * @return
     */
    public static boolean isUploadDataForArrive(List<ConfirmBean.ModelBean.ItemsBean>  list){
        if(list==null||list.size()==0){
            return false;
        }
        boolean flag=true;
        for (ConfirmBean.ModelBean.ItemsBean bean : list) {
            if(bean.isSelect()){
                int num=bean.getNumbers();
                int saomiaonum=Integer.parseInt(bean.getSaomiaonum());
                if(num< AppConstant.maxScanNum){
                    if(num!=saomiaonum)
                        return false;
                }
            }
        }
        return flag;
    }


    public static List<String> getLeakInfo(Context context,String filename,int num,String hdbh){
        String history= SharedPreferencesUtils.getParameter(context,filename);
        String[] histroyArray=history.split(",");
        List<String> historyList = new ArrayList<String>(Arrays.asList(histroyArray));
        List<String> t = new ArrayList<String>();
        String sub = "";
        for(int m=1;m<=num;m++){
            sub = String.format("%3s", m).replace(' ', '0');
            t.add(hdbh+sub);
        }
        t.removeAll(historyList);//实现了差集
        return t;
    }

    /**
     * 根据成功上传的单号去文件中删除相对应记录并保存文件
     * @param context
     * @param spName
     */
    public static void clearHistoryHdbh(Context context,String spName,List<TransportBean.ModelBean.ItemsBean> dataList){
        String history=SharedPreferencesUtils.getParameter(context,spName);
        String[] histroyArray=history.split(",");
        List<String> historyList=new ArrayList<String>(Arrays.asList(histroyArray));
        if(historyList.size()>0){
            for(int i=0;i<dataList.size();i++){
                if(dataList.get(i).isSelect()){
                    String temp=dataList.get(i).getHdbh();
                    Iterator<String> it = historyList.iterator();
                    while(it.hasNext()){
                        String oriStr=it.next();
                        if(TextUtils.isEmpty(oriStr)){
                            return;
                        }
                        String str ;
//                        str=oriStr.substring(0,12);
                        str=oriStr.substring(0,oriStr.length()-3);
                        if(str.equals(temp)){
                            it.remove();
                        }
                    }
                }
            }
        }
        //加到集合之后，我们需要在加上分隔符，然后存入文件中
        if(historyList.size()>0){
            StringBuilder builder=new StringBuilder();
            for (int k = 0; k <historyList.size();k++) {
                builder.append(historyList.get(k)+",");
            }
            SharedPreferencesUtils.saveParameter(context,spName,builder.toString());
        }else{
            SharedPreferencesUtils.deleteItem(context,spName);
        }
    }


    /**
     * 根据成功上传的单号去文件中删除相对应记录并保存文件
     * @param context
     * @param spName
     */
    public static void clearHistoryHdbhforArrive(Context context,String spName,List<ConfirmBean.ModelBean.ItemsBean> dataList){
        String history=SharedPreferencesUtils.getParameter(context,spName);
        String[] histroyArray=history.split(",");
        List<String> historyList=new ArrayList<String>(Arrays.asList(histroyArray));
        if(historyList.size()>0){
            for(int i=0;i<dataList.size();i++){
                if(dataList.get(i).isSelect()){
                    String temp=dataList.get(i).getHdbh();
                    Iterator<String> it = historyList.iterator();
                    while(it.hasNext()){
                        String oriStr=it.next();
                        if(TextUtils.isEmpty(oriStr)){
                            return;
                        }
                        String str ;
//                        str=oriStr.substring(0,12);
                        str=oriStr.substring(0,oriStr.length()-3);
                        if(str.equals(temp)){
                            it.remove();
                        }
                    }
                }
            }
        }
        //加到集合之后，我们需要在加上分隔符，然后存入文件中
        if(historyList.size()>0){
            StringBuilder builder=new StringBuilder();
            for (int k = 0; k <historyList.size();k++) {
                builder.append(historyList.get(k)+",");
            }
            SharedPreferencesUtils.saveParameter(context,spName,builder.toString());
        }else{
            SharedPreferencesUtils.deleteItem(context,spName);
        }
    }
}
